<template>
  <div class="analysis-card">
    <h3 class="analysis-title">基本顾客数据</h3>
    <div class="metrics-list">
      <div class="metric-item">
        <div class="metric-header">
          <span class="item-label">新客成交人数</span>
          <span :class="getData('采集新客人群数据-新客成交人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
            {{ getData('采集新客人群数据-新客成交人数-较上周期变化') }}
          </span>
        </div>
        <span class="amount">{{ getData('采集新客人群数据-新客成交人数') }}人</span>
        <div class="previous-value">上期: {{ getPreviousValues('采集新客人群数据-新客成交人数', '采集新客人群数据-新客成交人数-较上周期变化') }}</div>
      </div>

      <div class="metric-item">
        <div class="metric-header">
          <span class="item-label">老客成交人数</span>
          <span :class="getData('采集老客人群数据-老客成交人数-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
            {{ getData('采集老客人群数据-老客成交人数-较上周期变化') }}
          </span>
        </div>
        <span class="amount">{{ getData('采集老客人群数据-老客成交人数') }}人</span>
        <div class="previous-value">上期: {{ getPreviousValues('采集老客人群数据-老客成交人数', '采集老客人群数据-老客成交人数-较上周期变化') }}</div>
      </div>

      <div class="metric-item">
        <div class="metric-header">
          <span class="item-label">新客首单客单价</span>
          <span :class="getData('采集新客人群数据-新客首单均价-较上周期变化').includes('+') ? 'rate-up' : 'rate-down'">
            {{ getData('采集新客人群数据-新客首单均价-较上周期变化') }}
          </span>
        </div>
        <span class="amount">¥{{ getData('采集新客人群数据-新客首单均价') }}</span>
        <div class="previous-value">上期: ¥{{ getPreviousValues('采集新客人群数据-新客首单均价', '采集新客人群数据-新客首单均价-较上周期变化') }}</div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { getPreviousValue } from '../../../utils'
interface ReportItem {
  name: string
  value: string
}
const getPreviousValues = (currentKey: string, changeKey: string,): string => {

  const currentValue = getData(currentKey)
  const changeValue = getData(changeKey)
  return getPreviousValue(currentValue, changeValue)
}
interface ReportSection {
  title?: string
  items: ReportItem[]
}

interface Props {
  sectionData: ReportSection
}

const props = defineProps<Props>()

const getData = (key: string): string => {
  const item = props.sectionData.items.find((item: ReportItem) => item.name === key)
  return item?.value || '-'
}


</script>

<style scoped>
.analysis-card {
  margin-bottom: 16px;
  padding: 16px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.analysis-title {
  font-size: 16px;
  color: #2d3748;
  font-weight: 600;
  margin-bottom: 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid #e2e8f0;
}

.metrics-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.metric-item {
  padding: 8px;
  background: #f8fafc;
  border-radius: 6px;
}

.metric-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.item-label {
  font-size: 12px;
  color: #4a5568;
}

.amount {
  font-size: 18px;
  font-weight: 600;
  color: #2d3748;
}

.rate-up,
.rate-down {
  font-size: 11px;
  padding: 1px 4px;
  border-radius: 3px;
}

.rate-up {
  color: #22c55e;
  background: rgba(34, 197, 94, 0.1);
}

.rate-down {
  color: #ef4444;
  background: rgba(239, 68, 68, 0.1);
}

.previous-value {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .metrics-list {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 480px) {
  .metrics-list {
    grid-template-columns: 1fr;
  }
}
</style>